﻿using BJoin.Application.Dto;
using BJoin.Application.IManager;
using Microsoft.Extensions.DependencyInjection;
using BJoin.Application.WebApi.AutoDI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace BJoin.Application.WebApi.Middlewares
{
    public static class DependencyInjectionMiddleware
    {
        public static string[] Suffixs = new string[] { "Repository", "Manager", "Service" };
        public static IServiceCollection AutoDI(this IServiceCollection services)
        {
            return services.RegisterAssemblyPublicNonGenericClasses()
              .Where(c => Suffixs.Any(s => c.Name.EndsWith(s, StringComparison.InvariantCultureIgnoreCase)))
              .AsPublicImplementedInterfaces();
        }
        public static IServiceCollection AutoDI(this IServiceCollection services,Assembly[] assemblies)
        {
            return services.RegisterAssemblyPublicNonGenericClasses(assemblies)
              .Where(c => Suffixs.Any(s=>c.Name.EndsWith(s,StringComparison.InvariantCultureIgnoreCase)))
              .AsPublicImplementedInterfaces();
        }
    }
}
